require("shelljs/global");
// sudo npm i shelljs ora archiver -D

var path = require("path");
var fs = require("fs");
var archiver = require("archiver");
var archiveFilePath = path.join(__dirname, "../dist.zip");
var archive = archiver("zip");
var ora = require("ora");

if (!process.env.NODE_ENV) {
  console.log(
    "请指定打包的环境:NODE_EVN=production(生产环境)|NODE_ENV=test(测试环境)"
  );
  return;
}

rm("-rf", archiveFilePath);
// rm('-rf', path.join(__dirname, '../start.sh'))
// cp('-f', path.join(__dirname, '../start-' + process.env.NODE_ENV + '.sh'), path.join(__dirname, '../start.sh'))

var spinner = ora("archiving for " + process.env.NODE_ENV + "...");
spinner.start();

var output = fs.createWriteStream(archiveFilePath);

output.on("close", function () {
  spinner.stop();
  console.log(archive.pointer() + " total bytes");
  console.log(
    "archiver has been finalized and the output file descriptor has closed."
  );
});

archive.on("error", function (err) {
  throw err;
});

archive.pipe(output);

// archive.directory('node_modules', true, { date: new Date() });
archive.directory("dist", true, { date: new Date() });
archive.directory("deploy", { date: new Date() });
archive.directory("pem", { date: new Date() });
archive.file("package.json", { date: new Date() });
archive.file(".env", { date: new Date() });

archive.finalize();
